package com.henu.zmb;

import android.content.DialogInterface;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.henu.R;
import com.henu.zmb.DBhelper.DBhelper;
import com.henu.zmb.adapter.Goods_adapter;
import com.henu.zmb.entity.Goods;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class UserGoods_MainActivity extends AppCompatActivity {
    ListView lv_goods;
    Goods_adapter g_adapter;
    ArrayList<Goods> goods = new ArrayList<Goods>();
    ImageButton augm_return;
    Button augm_addgoods;
    protected void showgood() {
        goods.clear();
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        BmobQuery<Goods> query = new BmobQuery<Goods>();

        // 通过类别id查找当前分类下的所有物品
        query.addWhereEqualTo("User_Id", new BmobPointer(BmobUser.getCurrentUser()));
        query.include("User_Id");
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> object, BmobException e) {
                if(e==null){
                    goods = (ArrayList)object;
                    g_adapter.addAllData(goods); // 绑定数据
                    g_adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usergoods_main);
        augm_return = (ImageButton)findViewById(R.id.augm_return);
        lv_goods = (ListView)findViewById(R.id.augm_lvgoods);
        augm_addgoods = (Button)findViewById(R.id.augm_addgoods);
        g_adapter = new Goods_adapter(this,true);
        lv_goods.setAdapter(g_adapter);
        showgood();// 显示信息
        // 物品详情 点击事件
        lv_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(UserGoods_MainActivity.this,GoodsDetails_Activity.class); // 设置跳转的路径
                Goods good = goods.get(position); // 取出内容
                intent.putExtra("good", good); // 需要实例化Goods
                startActivity(intent); // 跳转
            }
        });
        // 删除物品
        lv_goods.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(UserGoods_MainActivity.this);
                dialog.setTitle("是否删除");
                dialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBhelper<Goods> db = new DBhelper<Goods>();
                        db.Delete(goods.get(position));
                        showgood();// 显示信息
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.create();
                dialog.show();
                return false;
            }
        });
        augm_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        augm_addgoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserGoods_MainActivity.this,AddGoods_Activity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
                finish();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        showgood();
    }
}
